package com.iwomy.secureplat.platform.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.iwomy.secureplat.common.core.util.R;
import com.iwomy.secureplat.common.excel.annotation.ResponseExcel;
import com.iwomy.secureplat.platform.dto.SensorTransmissionDataDto;
import com.iwomy.secureplat.platform.excel.HistoricalDataExcel;
import com.iwomy.secureplat.platform.service.HistoricalDataService;
import com.iwomy.secureplat.platform.transmission.entity.SensorTransmissionData;
import com.iwomy.secureplat.platform.transmission.entity.SensorTransmissionDataDetails;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 历史数据
 *
 * @author pig
 * @date 2024-01-29 09:58:08
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/historicalData")
@Tag(description = "historicalData", name = "历史数据")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class HistoricalDataController {

	private final HistoricalDataService historicalDataService;

	/**
	 * 获取指定所有条件数据
	 *
	 * @param sensorTransmissionDataDto
	 * @return
	 */
	@GetMapping("/list")
	public R historicalData(@ParameterObject SensorTransmissionDataDto sensorTransmissionDataDto) {
		try {
			return R.ok(historicalDataService.historicalData(sensorTransmissionDataDto));
		} catch (Exception e) {
			return R.failed(e.getMessage());
		}
	}

	/**
	 * 设备总览中曲线图数据
	 *
	 * @param sensorTransmissionDataDto
	 * @return
	 */
	@GetMapping("/deviceOverview")
	public R deviceOverview(@ParameterObject SensorTransmissionDataDto sensorTransmissionDataDto) {
		return historicalDataService.deviceOverview(sensorTransmissionDataDto);

	}

	/**
	 * 导出excel 表格
	 * @param sensorTransmissionDataDto 导出指定ID
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/export")
	public List<HistoricalDataExcel> export(@ParameterObject SensorTransmissionDataDto sensorTransmissionDataDto) {
		List<SensorTransmissionData> export = historicalDataService.export(sensorTransmissionDataDto);
		for (SensorTransmissionData sensorTransmissionData:export) {
			List<SensorTransmissionDataDetails> list = sensorTransmissionData.getSensorTransmissionDataDetailsList();
			if (ObjectUtil.isEmpty(list)) {
				sensorTransmissionData.setContent(null);
			} else {
				String content = list.stream()
						.map(details -> details.getGasTypeIdAndUnit() + ":" + details.getSensorVal())
						.collect(Collectors.joining("； "));
				sensorTransmissionData.setContent(content);
			}
		}
		List<HistoricalDataExcel> excelExport = BeanUtil.copyToList(export,HistoricalDataExcel.class);
		return excelExport;
	}

}